package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityApplyTojoinTheentErpriseActivityBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.CompanyInfoEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.AuthenticationEvent;
import com.sgb.kjwl.viewmodel.self.ApplyToJoinTheEnterpriseModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

public class ApplyToJoinTheEnterpriseActivity extends AppActivity {
    private ActivityApplyTojoinTheentErpriseActivityBinding binding;

    @Inject
    ApplyToJoinTheEnterpriseModel viewModel;
    private String companyNo;
    private boolean personAuth;
    private int type;
    private String id;
    private String comname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    //设置状态栏为暗
    @Override
    protected boolean statusBarDark() {
        return true;
    }

    //跳转
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ApplyToJoinTheEnterpriseActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, String comname,String companyNo,int type,String id) {
        Intent intent = new Intent();
        intent.putExtra("companyNo", companyNo);
        intent.putExtra("type",type);
        intent.putExtra("id",id);
        intent.putExtra("comname",comname);
        intent.setClass(context, ApplyToJoinTheEnterpriseActivity.class);
        context.startActivity(intent);
    }


    //获取屏幕焦点时
    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void initView() {
        //双向绑定Activity和View
        binding = DataBindingUtil.setContentView(ApplyToJoinTheEnterpriseActivity.this, R.layout.activity_apply_tojoin_theent_erprise_activity);
    }

    @Override
    protected void init() {
        //intent获取companyNo
        companyNo = getIntent().getStringExtra("companyNo");
        //身份
        type =getIntent().getIntExtra("type",1);
        id =getIntent().getStringExtra("id");
        comname= getIntent().getStringExtra("comname");
        //设置头部信息
        binding.include.titleView.setText("申请加入");
        binding.include.rightView.setText("    ");
        binding.include.rightView.setVisibility(View.GONE);
        //设置点击事件
        binding.include.rightView.setOnClickListener(this);
        //返回按钮的事件
        binding.include.ivBack.setOnClickListener(this);
        //请求公司详情
//        viewModel.getCompanyDetailModel(companyNo);
        binding.itemTvCompany.setContent(comname);
        //获取认证信息
        viewModel.getUserInfo();

        //判断不为空的情况下

        //点击触发
        binding.btSubmitApplication.setOnClickListener(this);
        binding.btSuccessfulReturn.setOnClickListener(this);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //提交按钮
            case R.id.bt_submit_application:
                if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
                    Toast.makeText(ApplyToJoinTheEnterpriseActivity.this, getResources().getText(R.string.real_auth_name_hint), Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!personAuth) {
                    if (TextUtils.isEmpty(binding.itemTvIdCard.getContent())) {
                        Toast.makeText(ApplyToJoinTheEnterpriseActivity.this, getResources().getText(R.string.real_auth_idcard_hint), Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                viewModel.applyCompanyModel(id,type,binding.itemTvCompany.getContent(), companyNo, binding.itemTvName.getContent(), binding.cfkRes.getEditContent(), binding.itemTvIdCard.getContent());
                break;
            //返回按钮
            case R.id.iv_back:
                //关闭当前界面
                finish();
                break;
            //申请成功返回按钮
            case R.id.bt_successful_return:
                //关闭当前界面
                finish();
                break;
        }
    }

    //获取公司信息
    public void showCompanyDetai(CompanyInfoEntity companyInfoEntity) {
        //不为空时，展示公司头像
        if (!TextUtils.isEmpty(companyInfoEntity.getCompLogo())) {
            Glide.with(this)
                    .load(companyInfoEntity.getCompLogo())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(binding.companyPortrait);
        } else {
            //为空时，展示默认头像
            binding.companyPortrait.setImageResource(R.drawable.icon_company_logo);
        }
        //设置公司简介
        binding.companyProfile.setText(companyInfoEntity.getName());
        binding.itemTvCompany.setContent(companyInfoEntity.getName());
    }

    public void showAuth(UserAuthMsgEntity data) {
        if (data.isPersonAuth()) {
            personAuth = true;
            binding.itemTvIdCard.setEnable(false);
            binding.itemTvName.setEnable(false);
            if (data.getRealName() != null) {
                //设置名字
                binding.itemTvName.setContent(data.getRealName());
                //设置光标在最后一位
                binding.itemTvName.setSelection(data.getRealName().length());
            }
            if (data.getIdCard() != null) {
                //设置身份证
                binding.itemTvIdCard.setContent(data.getIdCard());
                //设置光标在最后一位
                binding.itemTvIdCard.setSelection(data.getIdCard().length());
            }
            binding.itemTvIdCard.setVisibility(View.GONE);
        } else {
            binding.itemTvIdCard.setEnable(true);
            binding.itemTvName.setEnable(true);
            personAuth = false;
        }
    }

    //申请之后的操作，显示申请成功后的界面
    public void showApplyCompany() {
        binding.enterpriseApplicationLayout.setVisibility(View.GONE);
        binding.afterApplication.setVisibility(View.VISIBLE);
        EventBus.getDefault().postSticky(AuthenticationEvent.make(EventConstant.EVENT_JOIN_COM, 0));
    }
}
